const mta = require('../../../mta_sdk/mta_analysis.js');
const app = getApp();
var ctx=null;
var pid = '';

Page({

  data: {
    base64:'',
    shared: 0,
    txt:'',
    imgUrl:'',
    shareImg:'',
    ajaxSuccess:false,
    test:''
  },

  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.imgUrl && opt.txt){

      this.makeImg(opt.txt, opt.imgUrl)
      this.setData({
        txt: opt.txt,
        imgUrl: opt.imgUrl
      })
    }
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg || '',
        ajaxSuccess: true
      });

    }
    pid=opt.pid
  },

  makeImg(txt,imgUrl){
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    })
    var sysInfo =  wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .036;
    var query = wx.createSelectorQuery();
    ctx = wx.createCanvasContext("mycanvas");

    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {
      console.log("querysuccess")
      var w=res[0].width;
      var h =res[0].height;
      ctx.drawImage(imgUrl, 0, 0, w,h);
          ctx.setFontSize(ft);
          ctx.fillStyle = 'red';
          ctx.fillText(txt, w/7, h/3.35);
          ctx.draw(false,function(){
            app.buldingImg("mycanvas", callBack => {
              me.setData({
                shareImg: callBack.data.url,
                ajaxSuccess: true
              });
              wx.hideLoading();
            });
          });

    })
    
  },
  //保存图片
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    console.log(this.data.shareImg)
    return {
      title: '奖状制作',
      desc: '奖状制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
    }
  },
  toPlay() {
    wx.navigateTo({
      url: '/pages/jz/jz_make/jz_make?pid=jz',
    })
  },

  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
})